import { createRouter, createWebHistory, RouteRecordRaw } from 'vue-router'
import localCache from '@/utils/cache'

const routes: Array<RouteRecordRaw> = [
  {
    path: '/',
    redirect: '/login'
  },
  {
    path: '/login',
    name: 'login',
    component: () => import('@/views/login/login.vue')
  },
  {
    path: '/main',
    name: 'main',
    component: () => import('@/views/main/main.vue')
  },
  {
    path: '/not-found',
    name: 'notFound',
    component: () => import('@/views/not-found/not-found.vue')
  }
  // {
  //   path: '/main',
  //   name: 'main',
  //   component: () => import('@/views/main/main.vue'),
  //   children: [
  //     {
  //       path: 'system/user',
  //       name: 'user',
  //       component: () => import('@/views/main/system/user/user.vue')
  //     },
  //     {
  //       path: 'system/role',
  //       name: 'role',
  //       component: () => import('@/views/main/system/role/role.vue')
  //     },
  //     {
  //       path: 'system/department',
  //       name: 'department',
  //       component: () => import('@/views/main/system/department/department.vue')
  //     },
  //     {
  //       path: 'system/menu',
  //       name: 'menu',
  //       component: () => import('@/views/main/system/menu/menu.vue')
  //     }
  //   ]
  // }
]

const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes
})

router.beforeEach((to) => {
  if (to.path !== '/login') {
    const token = localCache.getCache('token')
    if (!token) {
      return '/login'
    }
  }

  if (to.path === '/main') {
    return '/main/system/user'
  }
})

export default router
